﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevEx.Thanh.reports;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraReports.UI;

namespace DevEx.Thanh.UserControl
{
    public partial class ucQuanLySanPhamTree : DevExpress.XtraEditors.XtraUserControl
    {
        QLVTDataContext dc = new QLVTDataContext();
        public ucQuanLySanPhamTree()
        {
            InitializeComponent();
        }
        public void KhoiTaotreelistSanPham()
        {
            CSanPham ctrlSanPham = new CSanPham();
            tlSanPham.LookAndFeel.UseDefaultLookAndFeel = false;
            tlSanPham.LookAndFeel.SkinName = "iMaginary";
            tlSanPham.OptionsBehavior.Editable = false;
            //tlSanPham.OptionsView.ShowColumns = false;
            tlSanPham.OptionsView.ShowIndicator = false;
            tlSanPham.OptionsView.ShowHorzLines = false;
            tlSanPham.OptionsView.ShowVertLines = false;
            
            tlSanPham.DataSource = ctrlSanPham.SelectAllRecord();
            tlSanPham.KeyFieldName = "ID_SanPham";
            tlSanPham.ParentFieldName = "ID_Parent";
            tlSanPham.Columns.ColumnByFieldName("KH_SanPham").Visible = false;
            tlSanPham.Columns.ColumnByFieldName("SanPham").Caption = "Sản phẩm";
        }
        
        public void KhoiTaodtgPhieuNhapXuat(int ID_SanPham)
        {
            var q = (from p in dc.tblDPNXes
                     join ct in dc.tblSLNXes on p.ID_DPNX equals ct.ID_DPNX
                     join t in dc.tblTons on ct.ID_MatHang equals t.ID_MatHang
                     where p.ID_SanPham==ID_SanPham && p.NX == "X"
                     select new
                     {
                         t.ID_MatHang,
                         t.tblVatTu.VatTu,t.tblVatTu.MaKyHieu,
                         t.tblVatTu.tblDVT.DVT,
                         t.Gia,
                         ct.SLKH,
                         ct.ThanhTien,
                         t.tblNguonVatTu.KH_NguonVatTu,
                         SoPhieu = (Convert.ToDateTime(p.NgayPhieu).Month > 9 ?
                            Convert.ToString(p.SoPhieu) + p.DuoiPhieu + "/" + Convert.ToDateTime(p.NgayPhieu).Month.ToString() + "-" + Convert.ToDateTime(p.NgayPhieu).Year.ToString().Substring(2, 2)
                            : Convert.ToString(p.SoPhieu) + p.DuoiPhieu + "/0" + Convert.ToDateTime(p.NgayPhieu).Month.ToString() + "-" + Convert.ToDateTime(p.NgayPhieu).Year.ToString().Substring(2, 2)),
                         p.NgayPhieu
                     })
            .GroupBy(g => new { g.ID_MatHang, g.VatTu, g.MaKyHieu, g.DVT, g.Gia, g.SLKH, g.ThanhTien, g.KH_NguonVatTu, g.SoPhieu, g.NgayPhieu })
            .Select(s => s.FirstOrDefault())
            .OrderBy (h => h.VatTu);

            dtgPhieuNhapXuat.DataSource = q;
            //FormatdtgPhieuNhapXuat();
        }
        public void FormatdtgPhieuNhapXuat()
        {
            var cl_mathang = gridView1.Columns.ColumnByFieldName("ID_MatHang");
            cl_mathang.Visible = false;
            
            //col so phieu
            var cl_Sp = gridView1.Columns.ColumnByFieldName("SoPhieu");
            cl_Sp.Width = 50;
            cl_Sp.Caption = "P.Xuất";
            cl_Sp.OptionsColumn.AllowEdit = false;
            cl_Sp.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_Sp.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns NgayPhieu
            var cl_NgayPhieu = gridView1.Columns.ColumnByFieldName("NgayPhieu");
            cl_NgayPhieu.Width = 50;
            cl_NgayPhieu.Caption = "Ngày tháng";
            cl_NgayPhieu.DisplayFormat.FormatType = FormatType.DateTime;
            cl_NgayPhieu.DisplayFormat.FormatString = "dd/MM/yyyy";
            cl_NgayPhieu.OptionsColumn.AllowEdit = false;
            cl_NgayPhieu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_NgayPhieu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //col ten vat tu
            var cl_VT = gridView1.Columns.ColumnByFieldName("VatTu");
            cl_VT.Width = 150;
            cl_VT.Caption = "Tên vật tư";
            cl_VT.OptionsColumn.AllowEdit = false;
            cl_VT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near ;
            cl_VT.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Near;

            //col Ma ky hieu
            var cl_Ma = gridView1.Columns.ColumnByFieldName("MaKyHieu");
            cl_Ma.Width = 100;
            cl_Ma.Caption = "Nhãn hiệu, quy cách";
            cl_Ma.OptionsColumn.AllowEdit = false;
            cl_Ma.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_Ma.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Near;

            //col DVT
            var cl_Dvt = gridView1.Columns.ColumnByFieldName("DVT");
            cl_Dvt.Width = 40;
            cl_Dvt.Caption = "ĐVT";
            cl_Dvt.OptionsColumn.AllowEdit = false;
            cl_Dvt.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center ;
            cl_Dvt.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns Gia
            var cl_Gia = gridView1.Columns.ColumnByFieldName("Gia");
            cl_Gia.Width = 50;
            cl_Gia.Caption = "Đơn giá";
            cl_Gia.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_Gia.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_Gia.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_Gia.DisplayFormat.FormatString = "#,###.##";

            //Columns So luong
            var cl_SL = gridView1.Columns.ColumnByFieldName("SLKH");
            cl_SL.Width = 50;
            cl_SL.Caption = "Số lượng";
            cl_SL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_SL.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_SL.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_SL.DisplayFormat.FormatString = "#,###.##";
            //Columns ThanhTien
            var cl_TongTien = gridView1.Columns.ColumnByFieldName("ThanhTien");
            cl_TongTien.Width = 80;
            cl_TongTien.Caption = "Thành tiền";
            cl_TongTien.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_TongTien.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_TongTien.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_TongTien.DisplayFormat.FormatString = "#,###.##";
            cl_TongTien.Summary.Add(DevExpress.Data.SummaryItemType.Sum, "ThanhTien", "{0:n0}");

            //col Nguon
            var cl_Nguon = gridView1.Columns.ColumnByFieldName("KH_NguonVatTu");
            cl_Nguon.Width = 50;
            cl_Nguon.Caption = "Nguồn";
            cl_Nguon.OptionsColumn.AllowEdit = false;
            cl_Nguon.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_Nguon.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
        }
        
        private void ucQuanLySanPham_Load(object sender, EventArgs e)
        {
            KhoiTaotreelistSanPham();
            FormatdtgPhieuNhapXuat();
        }

        private void btInDanhSach_Click(object sender, EventArgs e)
        {
            //lấy dữ liệu đổ ra dataset
            DataTable dt = new DataTable();
            string cauLenh = "select * from fc_T_LayThongTinNhapXuat() where NX ='X' ";
                     
            DevEx.Thanh.Utilities.XL_DULIEU.LayDuLieuRaBang(cauLenh, ref dt);
            dt.TableName = "fc_T_LayThongTinNhapXuat"; 
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            rptQLySanPham rpt = new rptQLySanPham();
            rpt.DataSource = ds;
            //set gia tri cho no
            rpt.RequestParameters = false; //hiển thị report luôn, không cần chờ nhập parametter
            ReportPrintTool pt = new ReportPrintTool(rpt);
            pt.AutoShowParametersPanel = false; //giấu phần hiển thị cho nhập parametter
            pt.ShowPreviewDialog();
        }

        private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            GridView view = (GridView)sender;
            //Check whether the indicator cell belongs to a data row
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (Convert.ToInt32(e.RowHandle) + 1).ToString();
                //if (!indicatorIcon)
                e.Info.ImageIndex = -1;
            }
        }

        private void btnInTheoSP_Click(object sender, EventArgs e){
            int SanPham = 0;
            try
            {
                SanPham = (Convert.ToInt32(tlSanPham.Selection[0].GetValue("ID_SanPham")));
            }
            catch { }
            DataTable dt = new DataTable();
            string cauLenh = "select * from fc_T_LayThongTinNhapXuat() where NX ='X' ";
            if (SanPham != 0)
            {
                cauLenh += " and ID_SanPham=" + SanPham;
            }   
            DevEx.Thanh.Utilities.XL_DULIEU.LayDuLieuRaBang(cauLenh, ref dt);
            dt.TableName = "fc_T_LayThongTinNhapXuat";
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            rptQLySanPham rpt = new rptQLySanPham();
            rpt.DataSource = ds;
            //set gia tri cho no
            rpt.RequestParameters = false; //hiển thị report luôn, không cần chờ nhập parametter
            ReportPrintTool pt = new ReportPrintTool(rpt);
            pt.AutoShowParametersPanel = false; //giấu phần hiển thị cho nhập parametter
            pt.ShowPreviewDialog();
        }

        private void tlSanPham_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            try
            {
                int idSanPham = Convert.ToInt32(e.Node.GetValue("ID_SanPham"));
                KhoiTaodtgPhieuNhapXuat(idSanPham);
            }
            catch
            {
            }        
        }
       
    }
}
